package com.wb.oldwomen;

import android.view.MotionEvent;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wb.fw.SoundManager;
import com.wb.fw.Util;
import com.wb.oldwomen.GamePlayLayer.ResultListener;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor4B;

public class UIResult extends ColorLayer implements IConstants, ResultListener {
	public static UIResult make(GameScene parent) {
		return new UIResult(parent, WYColor4B.make(0, 0, 0, 127));
	}
	GameScene mparent;
	protected UIResult(GameScene parent, WYColor4B color) {
		super(color);
		mparent = parent;
		setKeyEnabled(true);
		setTouchEnabled(true);
	}
	
	@Override
	protected boolean onBackButton() {
		if (isVisible()) {
			removeChild(mbg, true);
			removeChild(mhit, true);
			removeChild(mscore, true);
			removeChild(mgold, true);
			mparent.switchState(GS_GAME_LEVEL_SELECT);
		}
		return true;
	}
	
	@Override
	public boolean wyTouchesBegan(MotionEvent event) {
		return true;
	}

	Sprite mbg,mhit;
	Label mscore, mgold;
	float mdelay = 5.0f;
	@Override
	public void onresult(int... result) {
		SoundManager.playEffect(R.raw.win);
		int apple = result[0];
		int gold = result[1];
		int score = apple*300+gold*50;
		int lastgold = Util.getGold(GateActivity.sInstance);
		Util.saveGold(GateActivity.sInstance, gold+lastgold);
		if (apple >= 3) {
			apple = 3;
			mparent.unlockNext();
		}
		mbg = Sprite.make(R.drawable.finish_0+apple);
		mbg.autoRelease();
		mbg.setScale(Global.sScaleRate);
		mbg.setAnchor(0, 0);
		mbg.setPosition(Global.sStartX, Global.sEndY);
		addChild(mbg);
		
		mhit = Sprite.make(apple==3?R.drawable.suc:R.drawable.fail);
		mhit.autoRelease();
		mhit.setScale(Global.sScaleRate);
		mhit.setPosition(Global.sWYSize.width/2, Global.sWYSize.height*2/3);
		addChild(mhit);
		
		mscore = Label.make(String.valueOf(score), 30);
		mscore.autoRelease();
		mscore.setAnchor(1, 0.5f);
		mscore.setPosition(Global.sStartX+200*Global.sScaleRate, Global.sEndY+50*Global.sScaleRate);
		addChild(mscore);
		
		mgold = Label.make(String.valueOf(gold), 30);
		mgold.autoRelease();
		mgold.setAnchor(1, 0.5f);
		mgold.setPosition(Global.sEndX-96*Global.sScaleRate, Global.sEndY+50*Global.sScaleRate);
		addChild(mgold);
		
		
		mdelay = 5.0f;
		mparent.switchState(GS_GAME_RESULT);
	}

	public void tick(float delta) {
//		mdelay -= delta;
//		if (mdelay < 0) {
//			onBackButton();
//		}
	}
}
